
$(function() {
	Tip.emp('员工');

	_change("input[id^='name_s']:visible", "select[id^='name_t']:visible");
});

var Salary = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() { 
				$(this).dialog("close");
			},			
			
			"添加": function() {				
				if (!confirm("确认是否添加？")) {
					return false;
				}				
	
				var form = $("#form_add")[0];
	
				var body = { };
				body.emp = form.emp.value;
				body.name = form.name.value;
				body.type = form.type.value;
				
				body.payment = form.payment.value;
				body.content = form.content.value;
				
				body.d1 = form.d1.value;
				body.d2 = form.d2.value;
				body.d3 = form.d3.value;
				body.d4 = form.d4.value;
				body.d5 = form.d5.value;
				
				body.s1 = form.s1.value;
				body.s2 = form.s2.value;
				body.s3 = form.s3.value;
				body.s4 = form.s4.value;
				
				body.k1 = form.k1.value;
				body.k2 = form.k2.value;
				body.k9 = form.k9.value;
				
				body.kd = form.kd.value;
				body.t1 = form.t1.value;
				body.t2 = form.t2.value;
				
				body.ns = form.ns.value;
				body.sj = form.sj.value;
				
				// alert($.toJSON(body));
					
				$(this).dialog("close");
	
				$.post('finance.php?do=Salary.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	dialog_import: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('finance.php?do=Salary.audit&item=146', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},
		
					"导入": function() {
						if (!confirm("确认是否导入？")) {
							return false;
						}
						
						var form = $("#form_add")[0];
		
						var body = { };					
						var aa = form.way.value;
						
						body.name = form.name.value;
						body.type = form.type.value;
						body.oper = form.oper.value;
						body.data = form.data.value;
						
						if (aa != null && aa == '2') {
							$('#userfile').uploadify('upload');
							
							return false;
						}
		
						$(this).dialog("close");
		
						$.post('finance.php?do=Salary.importPost', body, function(data) {
							$(result).html(data);
						});
					}
				});
		
				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});	
	},
	
	popup: function (e, p2, id, status, emp) {
		$.cookie('id_', id);
		$.cookie('emp_', emp);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'finance.php?do=Salary.edit&id=' + id;
		var c2 = 'finance.php?do=Salary.view&id=' + id;		
		
		var cs = 'finance.php?do=Salary.status&id=' + id + p2;
		
		var a1 = ['编辑信息', 'url', 'Salary.dialog_edit_', '700x450'];	
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x420'];
					
		if (status == '1') {
			var d1 = 'finance.php?do=Salary.verify&id=' + id;
			var b1 = ['审核信息', 'url', 'Salary.dialog_verify', '650x400'];
			
			var a2 = ['查看信息', 'url', 'Salary.dialog_status_1', '650x420'];
			
			var s = d1 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}
		
		if (status == '2') {
			var b1 = ['开始发放', 'url', 'Salary.list_start'];
			
			var s = cs + "&status=3, " + c2;

			return this.popup_(e, [b1, a2], s);
		}
		
		if (status == '3') {
			var d1 = 'finance.php?do=Salary.finish&id=' + id;
			var b1 = ['完成发放', 'url', 'Salary.dialog_finish', '650x400'];
			
			var s = d1 + ", " + c2;

			return this.popup_(e, [b1, a2], s);
		}

		var s = c1 + ", " + c2;

		return this.popup_(e, [a1, a2], s);
	},

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		var emp = $.cookie('emp_');
		
		var c2 = 'finance.php?do=Salary.upload&id=' + id;
		var a2 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var c3 = 'finance.php?do=Salary.copy&id=' + id;
		var a3 = ['复制信息', 'url', 'Salary.dialog_copy_', '650x420'];
		
		var c4 = 'finance.php?do=Salary.history&id=' + id;
		var a4 = ['历史工资', 'url', 'Common.dialog_view_', '760x420'];
		
		var c5 = 'emp.php?do=Salary.view_emp&emp=' + emp;
		var a5 = ['工资信息', 'url', 'Common.dialog_view_', '760x420'];
		
		var c6 = 'kq.php?do=Vacation.view_emp&emp=' + emp;
		var a6 = ['请假信息', 'url', 'Common.dialog_view_', '760x420'];
		
		var ss = 'finance.php?do=Salary.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Salary.dialog_ss', '400x200'];
		
		s += ", " + c2 + ", " + c3 + ", " + c4 + ", " + c5 + ", " + c6 + ", " + ss;
		
		a.push(a2);
		a.push(a3);
		a.push(a4);
		a.push(a5);
		a.push(a6);
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		result = Common.result();
		$.getJSON('finance.php?do=Salary.audit&item=145', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'finance.php?do=Salary.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_edit")[0];
				
				var body = { };
				body.id = form.id.value;
				body.emp = form.emp.value;
				
				var zh_x = {};
				zh_x.bank = form.zh_x_bank.value;
				zh_x.name = form.zh_x_name.value;
				zh_x.code = form.zh_x_code.value;
				zh_x.content = form.zh_x_content.value;
				
				body.ns_x = $.toJSON(zh_x);
				
				var gh_x = {};
				gh_x.bank = form.gh_x_bank.value;
				gh_x.name = form.gh_x_name.value;
				gh_x.code = form.gh_x_code.value;
				gh_x.content = form.gh_x_content.value;
				
				body.sj_x = $.toJSON(gh_x);
				
				body.name = form.name.value;
				body.type = form.type.value;
				body.payment = form.payment.value;
				body.content = form.content.value;
				body.status = form.status.value;
				
				body.d1 = form.d1.value;
				body.d2 = form.d2.value;
				body.d3 = form.d3.value;
				body.d4 = form.d4.value;
				body.d5 = form.d5.value;
				
				body.s1 = form.s1.value;
				body.s2 = form.s2.value;
				body.s3 = form.s3.value;
				body.s4 = form.s4.value;
				
				body.k1 = form.k1.value;
				body.k2 = form.k2.value;
				body.k9 = form.k9.value;
				
				body.kd = form.kd.value;
				body.t1 = form.t1.value;
				body.t2 = form.t2.value;
				
				body.ns = form.ns.value;
				body.sj = form.sj.value;
				
				// alert($.toJSON(body));
				
				$(this).dialog("close");
	
				var url = 'finance.php?do=Salary.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() { 
				$(this).dialog("close");
			},			
			
			"复制": function() {				
				if (!confirm("确认是否提交？")) {
					return false;
				}				
	
				var form = $("#form_add")[0];
	
				var body = { };
				body.emp = form.emp.value;
				body.name = form.name.value;
				body.type = form.type.value;
				
				body.payment = form.payment.value;
				body.content = form.content.value;
				
				body.d1 = form.d1.value;
				body.d2 = form.d2.value;
				body.d3 = form.d3.value;
				body.d4 = form.d4.value;
				body.d5 = form.d5.value;
				
				body.s1 = form.s1.value;
				body.s2 = form.s2.value;
				body.s3 = form.s3.value;
				body.s4 = form.s4.value;
				
				body.k1 = form.k1.value;
				body.k2 = form.k2.value;
				body.k9 = form.k9.value;
				
				body.kd = form.kd.value;
				body.t1 = form.t1.value;
				body.t2 = form.t2.value;
				
				body.ns = form.ns.value;
				body.sj = form.sj.value;
				
				// alert($.toJSON(body));
					
				$(this).dialog("close");
	
				$.post('finance.php?do=Salary.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	dialog_status_1: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_view').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"打印": function() { 
				$.jPrintArea("#dialog_view");
				//  $(this).dialog("close");
			},
			
			"完成制作": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
				
				$(this).dialog("close");
								
				var body = { };
				body.id = $.cookie('id_');
				body.status = 2;
				
				var url = 'finance.php?do=Salary.status';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
				
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_view(url, title, xy);
	},
	
	dialog_verify: function (url, title, xy) {
		result = Common.result();
		$.getJSON('finance.php?do=Salary.audit&item=146', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不同意": function() {
						if (!confirm("确认是否不同意？")) {
							return false;
						}				
						
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 9;
						
						$(this).dialog("close");
						
						var url = 'finance.php?do=Salary.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"同意": function() {
						if (!confirm("确认是否同意？")) {
							return false;
						}				

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 2;
						
						$(this).dialog("close");

						var url = 'finance.php?do=Salary.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},
	
	dialog_finish: function (url, title, xy) {
		result = Common.result();
		$.getJSON('finance.php?do=Salary.audit&item=146', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"完成发放": function() {
						if (!confirm("确认是否完成发放？")) {
							return false;
						}				

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.email = form.email.value;
						body.content = form.content.value;
						
						$(this).dialog("close");

						var url = 'finance.php?do=Salary.finishPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},
		
	list_start: function (url, view) {
		$.getJSON('finance.php?do=Salary.audit&item=151', function(json) {
			if (json.status == 1) {
				if (confirm("是否要开始发放？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);
		
		this.bank(e);
	},
	
	abc: function (json, e) {

		var p2 = $.cookie('p2');
		// alert(p2);
		
		var c1 = 'finance.php?do=Salary.delete' + p2;
		
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		
		var s = c1;
		var a = [a1];
		
		for (i = 0; i < json.length; i++) {
			// alert(json[i].id + ", " + json[i].bank + ", " + json[i].type);
			
			if (json[i].bank == '招商银行' && json[i].type == '1') {
				var c2 = 'finance.php?do=Salary.txt' + p2;
				var a2 = ['个人招行', 'url', 'Common.dialog_txt', '650x400'];
				
				s += ", " + c2;
				a.push(a2);
			}
			
			if (json[i].bank == '招商银行' && json[i].type == '2') {
				var c2 = 'finance.php?do=Salary.txt&bank=cmb' + p2;
				var a2 = ['公司招行', 'url', 'Common.dialog_txt', '650x400'];
				
				s += ", " + c2;
				a.push(a2);
			}
			
			if (json[i].bank == '东莞农村商业银行' && json[i].type == '2') {
				var c2 = 'finance.php?do=Salary.xls_drc' + p2;
				var a2 = ['东莞农村商业银行', 'url', 'Common.dialog_txt', '650x400'];
				
				s += ", " + c2;
				a.push(a2);
			}
			
			if (json[i].bank == '广发银行' && json[i].type == '1') {
				var c2 = 'finance.php?do=Salary.xls_cgb' + p2;
				var a2 = ['个人广发', 'url', 'Common.dialog_txt', '650x400'];
				
				s += ", " + c2;
				a.push(a2);
			}
			
			if (json[i].bank == '中国工商银行' && json[i].type == '2') {
				// var a3 = ['工行转帐', 'url', 'Common.dialog_txt', '650x400'];
				// var c3 = 'finance.php?do=Salary.icbc' + p2;
				
				var c2 = 'finance.php?do=Salary.xls_icbc' + p2;
				var a2 = ['工行Excel', 'url', 'Common.dialog_txt', '650x400'];
				
				s += ", " + c2;
				a.push(a2);
			}
			
			if (json[i].bank == '中国民生银行' && json[i].type == '1') {
				var c2 = 'finance.php?do=Salary.txt&bank=cmbc' + p2;
				var a2 = ['个人民生', 'url', 'Common.dialog_txt', '650x400'];
				
				s += ", " + c2;
				a.push(a2);
			}
			
			if (json[i].bank == '中国民生银行' && json[i].type == '2') {
				var c2 = 'finance.php?do=Salary.txt&bank=cmbc' + p2;
				var a2 = ['公司民生', 'url', 'Common.dialog_txt', '650x400'];
				
				s += ", " + c2;
				a.push(a2);
			}
			
			if (json[i].bank == '平安银行' && json[i].type == '1') {
				var c2 = 'finance.php?do=Salary.xls_pa' + p2;
				var a2 = ['个人平安', 'url', 'Common.dialog_txt', '650x400'];
				
				s += ", " + c2;
				a.push(a2);
			}
			
			if (json[i].bank == '北京银行' && json[i].type == '2') {
				var c2 = 'finance.php?do=Salary.bj' + p2;
				var a2 = ['北京银行', 'url', 'Common.dialog_txt', '650x400'];
				
				s += ", " + c2;
				a.push(a2);
			}
			
			if (json[i].bank == '中信银行' && json[i].type == '2') {
				var c2 = 'finance.php?do=Salary.txt&bank=citic' + p2;
				var a2 = ['中信银行', 'url', 'Common.dialog_txt', '650x400'];
				
				s += ", " + c2;
				a.push(a2);
			}
		}
			
		var c4 = 'finance.php?do=Salary.batch_begin';
		var c5 = 'finance.php?do=Salary.batch_finish';
		
		
		var a4 = ['开始发放', 'url', 'Salary.dialog_batch_begin', '800x450'];
		var a5 = ['完成发放', 'url', 'Salary.dialog_batch_finish', '800x450'];
		
		var c9a = 'finance.php?do=Salary.xls&' + p2;
		var c9b = 'finance.php?do=Salary.xls&xls=2' + p2;
		var c9c = 'finance.php?do=Salary.xls&xls=3' + p2;
		var c9d = 'finance.php?do=Salary.xls&xls=4' + p2;
		
		var c9e = 'finance.php?do=Salary.batch_file';
		
		var a9a = ['导出Excel', 'url', 'jswin', '1024x768'];
		var a9b = ['导出Excel2', 'url', 'jswin', '1024x768'];
		var a9c = ['导出Excel3', 'url', 'jswin', '1024x768'];
		var a9d = ['签收单', 'url', 'jswin', '1024x768'];
		var a9e = ['批量包', 'url', 'Salary.dialog_batch_file', '800x450'];
		
		s += ", " + c4 + ", " + c5 + ", " + c9a + ", " + c9b + ", " + c9c + ", " + c9d + ", " + c9e;
		
		a.push(a4);
		a.push(a5);
		
		a.push(a9a);
		a.push(a9b);
		a.push(a9c);
		a.push(a9d);
		a.push(a9e);
		
		return popup(e, a, s);
	},
	
	bank: function (e) {
		var body = { };
		body.service = 1;
		
		$.getJSON('emp.php?do=BankMy.ajax_my', body, function(json) {
			// alert(e);
			Salary.abc(json, e);
		});
	},
	
	dialog_batch_begin: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('finance.php?do=Salary.audit&item=151', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_2_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},					
					
					"开始发放": function() {
						if (!confirm("确认是否开始发放？")) {
							return false;
						}
						
						var cc = Salary._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.content = form.content.value;					
						body.status = 3;
						
						var url = 'finance.php?do=Salary.batch_beginPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},
	
	dialog_batch_finish: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('finance.php?do=Salary.audit&item=151', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_1_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				$("input[name^='c_2_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				$("input[name^='c_3_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},					
					
					"完成发放": function() {
						if (!confirm("确认是否完成发放？")) {
							return false;
						}
						
						var cc = Salary._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.finish = form.finish_dd.value + " " + form.finish_tt.value;
						body.content = form.content.value;
						
						var url = 'finance.php?do=Salary.batch_finishPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},
	
	dialog_batch_file: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('finance.php?do=Salary.audit&item=151', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},					
					
					"提交": function() {
						if (!confirm("确认是否开始提交？")) {
							return false;
						}
						
						var cc = Salary._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						body.cc = cc;
						body.file = form.file.value;
						body.content = form.content.value;					
						body.status = 3;
						
						var url = 'finance.php?do=Salary.batch_filePost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},
	
	_cc: function () {
		var c = new Array();
		$("input[name^='b_']").each(function() {
			if ($(this).attr("checked")) {
				c.push($(this).val());
			}
		});
		
		return c;
	}
};
